﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Text;

namespace MVCBugTracker.Models
{
    public partial class Bug : ICloneable
    {
        public string GetBugChanges(List<object[]> changes)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table>");
            sb.Append("<tr><th>Field</th><th>Old Value</th><th>New Value</th></tr>");

            foreach (object[] change in changes)
            {
                object propertyName = change[0] ?? "";
                object originalPropertyValue = change[1] ?? "";
                object newPropertyValue = change[2] ?? "";

                if (originalPropertyValue.ToString() != newPropertyValue.ToString())
                {
                    sb.Append("<tr>");
                    sb.Append(string.Format("<td>{0}</td>", propertyName));
                    sb.Append(string.Format("<td>{0}</td>", originalPropertyValue));
                    sb.Append(string.Format("<td>{0}</td>", newPropertyValue));
                    sb.Append("</tr>");
                }
            }

            //hack: removed reflection history
            //foreach (PropertyInfo property in originalBug.GetType().GetProperties())
            //{
            //    Type comparable = property.PropertyType.GetInterface("System.IComparable");
            //    if (comparable != null)
            //    {
            //        object originalPropertyValue = property.GetValue(originalBug, null) ?? "";
            //        object newPropertyValue = property.GetValue(changedBug, null) ?? "";
            //            sb.Append("<tr>");
            //            sb.Append(string.Format("<td>{0}</td>", property.Name));
            //            sb.Append(string.Format("<td>{0}</td>", originalPropertyValue));
            //            sb.Append(string.Format("<td>{0}</td>", newPropertyValue));
            //            sb.Append("</tr>");
            //        }
            //    }
            //}

            sb.Append("</table>");

            return sb.ToString();
        }

        public object Clone()
        {    
            return this.MemberwiseClone();
        }

    }
}